package com.alibaba.citrus.ots.platform.enhance.repository.impl;

import com.alibaba.citrus.ots.platform.enhance.repository.InventoryAllotRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.enums.DimensionTypeEnum;
import com.epoch.app.otsplatformenhance.inventoryallot.dto.CreateResponse;
import com.epoch.app.otsplatformenhance.inventoryallot.dto.DeleteRequest;
import com.epoch.app.otsplatformenhance.inventoryallot.dto.UpdateToInvalidRequest;
import com.epoch.app.otsplatformenhance.inventoryallot.model.InventoryAllot;
import com.epoch.app.otsplatformenhance.inventoryallot.service.InventoryAllotService;
import com.epoch.app.otsplatformenhance.inventoryallotline.dto.DeleteByIdRequest;
import com.epoch.app.otsplatformenhance.inventoryallotline.model.InventoryAllotLine;
import com.epoch.app.otsplatformenhance.inventoryallotline.service.InventoryAllotLineService;
import com.epoch.app.otsplatformenhance.model.dto.InventoryAllotLineLoadListRequest;
import com.epoch.app.otsplatformenhance.model.dto.InventoryAllotSDO;
import com.epoch.app.otsplatformenhance.model.dto.QueryDuctManualAllotResponse;
import com.epoch.app.otsplatformenhance.model.dto.QueryManualAllotRequest;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InventoryAllotRepositoryImpl implements InventoryAllotRepository {

    private final Logger logger= LoggerFactory.getLogger(InventoryAllotRepositoryImpl.class);

    @Autowired
    private InventoryAllotService inventoryAllotService;
    @Autowired
    private InventoryAllotLineService inventoryAllotLineService;

    @Override
    public String create(InventoryAllotSDO inventoryAllotSDO) {
        InventoryAllot inventoryAllot = getInventoryAllot(inventoryAllotSDO);
        CreateResponse createResponse = inventoryAllotService.create(inventoryAllot);
        return createResponse.getId();
    }

    @Override
    public void update(InventoryAllotSDO inventoryAllotSDO) {
        InventoryAllot inventoryAllot = getInventoryAllot(inventoryAllotSDO);
        inventoryAllot.setInventoryAllotTime(inventoryAllotSDO.getInventoryAllotTime());
        inventoryAllot.setId(Long.parseLong(inventoryAllotSDO.getId()));
        inventoryAllotService.update(inventoryAllot);
    }

    @Override
    public void delete(InventoryAllotSDO inventoryAllotSDO) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.setId(Long.parseLong(inventoryAllotSDO.getId()));
        inventoryAllotService.delete(deleteRequest);

        DeleteByIdRequest deleteByIdRequest = new DeleteByIdRequest();
        deleteByIdRequest.setInventoryAllotId(inventoryAllotSDO.getId());
        inventoryAllotLineService.deleteById(deleteByIdRequest);
    }

    @Override
    public QueryDuctManualAllotResponse queryManualImportAllot(QueryManualAllotRequest request) {
        List<String> dimensionTypeList = Lists.newArrayList(DimensionTypeEnum.CHANNEL.getCode(),DimensionTypeEnum.DEPARTMENT.getCode(),
                DimensionTypeEnum.SALESMAN.getCode());

        Map<String, String> manualDuctMap = new HashMap<>();
        QueryDuctManualAllotResponse response = new QueryDuctManualAllotResponse();
        QueryManualAllotRequest allotLoadListRequest = new QueryManualAllotRequest();
        allotLoadListRequest.setOrganizationCode(request.getOrganizationCode());
        for (String dimensionType:dimensionTypeList) {
            allotLoadListRequest.setDimensionType(dimensionType);
            Result<List<InventoryAllot>> inventoryAllotResult = inventoryAllotService.queryManualAllotList(allotLoadListRequest);
            if (inventoryAllotResult == null || CollectionUtils.isEmpty(inventoryAllotResult.getResult())) {
                continue;
            }
            InventoryAllot allot = inventoryAllotResult.getResult().get(0);
            InventoryAllotLineLoadListRequest allotLineRequest = new InventoryAllotLineLoadListRequest();
            allotLineRequest.setInventoryAllotId(String.valueOf(allot.getId()));
            Result<List<InventoryAllotLine>> result = inventoryAllotLineService.loadInventoryAllotLineList(allotLineRequest);
            if (result == null || CollectionUtils.isEmpty(result.getResult())) {
                continue;
            }
            result.getResult().stream().forEach(inventoryAllotLine -> {
                BigDecimal dis = inventoryAllotLine.getDistributionQuantity() == null ? BigDecimal.ZERO : new BigDecimal(inventoryAllotLine.getDistributionQuantity());
                if (dis.intValue() < 0) {
                    dis = BigDecimal.ZERO;
                }
                manualDuctMap.put(inventoryAllotLine.getScItemId(), manualDuctMap.get(inventoryAllotLine.getScItemId()) == null ? dis.toString() :new BigDecimal(manualDuctMap.get(inventoryAllotLine.getScItemId())).add(dis).toString());
            });
        }
        response.setManualDuctMap(manualDuctMap);
        return response;
    }

    @Override
    public Integer updateInventoryAllotToInvalid() {
        inventoryAllotService.updateToInvalid(new UpdateToInvalidRequest());
        return null;
    }

    @NotNull
    private InventoryAllot getInventoryAllot(InventoryAllotSDO inventoryAllotSDO) {
        InventoryAllot inventoryAllot = new InventoryAllot();
        inventoryAllot.setOrganizationCode(inventoryAllotSDO.getOrganizationCode());
        inventoryAllot.setOrganizationName(inventoryAllotSDO.getOrganizationName());
        inventoryAllot.setDistributionId(inventoryAllotSDO.getDistributionId());
        inventoryAllot.setDistributionType(inventoryAllotSDO.getDistributionType());
        inventoryAllot.setValidBeginTime(inventoryAllotSDO.getValidBeginTime());
        inventoryAllot.setValidEndTime(inventoryAllotSDO.getValidEndTime());
        inventoryAllot.setDailyBeginTime(inventoryAllotSDO.getDailyBeginTime());
        inventoryAllot.setDailyEndTime(inventoryAllotSDO.getDailyEndTime());
        inventoryAllot.setInventoryAllotTime(inventoryAllotSDO.getInventoryAllotTime());
        inventoryAllot.setFeatures(inventoryAllotSDO.getFeatures());
        inventoryAllot.setLimitSwitch(inventoryAllotSDO.getLimitSwitch());
        return inventoryAllot;
    }


}
